﻿using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.ObjectMapping;
using CPPProject.CPP.Dto;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace CPPProject.CPP
{
    public class CPPOrderHedAppService : AsyncCrudAppService<CPPOrderHed, CPPOrderHedDto, Guid, PagedResultRequestDto, CreateCPPOrderHedDto, UpdateCPPOrderHedDto>
    {
        private readonly IObjectMapper _objectMapper;
        private readonly ICPPOrderDtlAppService _cPPOrderDtlAppService;
        private readonly ICPPCustomerAppService _cPPCustomerAppService;
        public CPPOrderHedAppService(
                    IRepository<CPPOrderHed, Guid> repository,
                    IObjectMapper objectMapper,
                    ICPPOrderDtlAppService cPPOrderDtlAppService,
                    ICPPCustomerAppService cPPCustomerAppService) : base(repository)
        {
            _objectMapper = objectMapper;
            _cPPOrderDtlAppService = cPPOrderDtlAppService;
            _cPPCustomerAppService = cPPCustomerAppService;
        }
        /// <summary>
        /// 查询订单
        /// </summary>
        /// <param name="skipCount"></param>
        /// <param name="maxResultCount"></param>
        /// <param name="orderNum">订单编号</param>
        /// <param name="startDate">开始时间</param>
        /// <param name="endDate">结束时间</param>
        /// <param name="custNum">客户编码</param>
        /// <param name="iType">类型（1发出2回收）</param>
        /// <param name="status">订单状态（0待审核1确认审核2确认完成3已关闭4审核中）</param>
        /// <returns></returns>
        public async Task<PagedResultDto<CPPOrderHedDto>> GetList(int skipCount = 0, int maxResultCount = 10, string orderNum = "", string startDate = "", string endDate = "", string custNum = "", string iType = "", string status = "")
        {
            PagedResultDto<CPPOrderHedDto> resultDto = new PagedResultDto<CPPOrderHedDto>();
            List<CPPContract> lists = new List<CPPContract>();
            Expression<Func<CPPOrderHed, bool>> expr = o => 1 == 1;
            if (!string.IsNullOrEmpty(orderNum))
            {
                expr = expr.ExpressionAnd(o => o.ContractNum == orderNum);
            }
            if (!string.IsNullOrEmpty(startDate))
            {
                expr = expr.ExpressionAnd(o => o.OrderDate.Day.ToString() == startDate);
            }
            if (!string.IsNullOrEmpty(endDate))
            {
                expr = expr.ExpressionAnd(o => o.DueDate.Day.ToString() == endDate);
            }
            if (!string.IsNullOrEmpty(custNum))
            {
                expr = expr.ExpressionAnd(o => o.CustNum == custNum);
            }
            if (!string.IsNullOrEmpty(iType))
            {
                expr = expr.ExpressionAnd(o => o.IType == iType);
            }
            if (!string.IsNullOrEmpty(status))
            {
                expr = expr.ExpressionAnd(o => o.Status == status);
            }
            if (expr != null)
            {
                var dtos = await Repository.GetAll().Where(expr).OrderByDescending(o => o.CreationTime).Skip(skipCount).Take(maxResultCount).ToListAsync();
                resultDto.Items = _objectMapper.Map<List<CPPOrderHedDto>>(dtos);
                resultDto.TotalCount = lists.Count;
                foreach (var item in resultDto.Items)
                {
                    item.dtlDtos = await _cPPOrderDtlAppService.GetById(item.OrderNum);
                    var com = await _cPPCustomerAppService.GetById(item.CustNum);
                    item.Name = com.Name;
                }
            }
            else
            {
                resultDto = await base.GetAllAsync(new PagedResultRequestDto { MaxResultCount = maxResultCount, SkipCount = skipCount });
            }
            return resultDto;
        }
        /// <summary>
        /// 批量删除订单
        /// </summary>
        /// <param name="id">标识Id</param>
        /// <param name="dtlid">订单明细标识Id</param>
        /// <returns></returns>
        public async Task BatchDelete(string id, string dtlid)
        {
            if (!string.IsNullOrEmpty(id))
            {
                await Repository.DeleteAsync(o => id.Contains(o.Id.ToString()));
            }
            if (!string.IsNullOrEmpty(dtlid))
            {
                await _cPPOrderDtlAppService.BatchDeleteDtl(dtlid);//删除合同明细
            }
        }
        /// <summary>
        /// 批量修改状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="state">订单状态（0待审核1确认审核2确认完成3已关闭4审核中）</param>
        /// <returns></returns>
        public async Task<bool> BatchChangeStatus(string id, string state)
        {
            string[] obj = id.Split(",");
            for (int i = 0; i < obj.Length; i++)
            {
                var list = await Repository.GetAll().Where(o => o.Id.ToString() == obj[i]).FirstOrDefaultAsync();
                list.Status = state;
            }
            return true;
        }
    }
}
